﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace BabyNames
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private List<BabyName> babyNames = new List<BabyName>();

        public MainWindow()
        {
            // create reader & open file
            var tr = new StreamReader("babynames.txt");
            while (!tr.EndOfStream)
                babyNames.Add(new BabyName(tr.ReadLine()));

            tr.Close();

            InitializeComponent();
        }

        private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            var decade = Convert.ToInt32(((ListBoxItem) sender).Content.ToString());
            babyNames.Sort(new BabySortByDecade(decade));
            
            lstDecadeTopNames.Items.Clear();
            
            for (int i = 0; i < 20; i++)
            {
                lstDecadeTopNames.Items.Add(babyNames[i].Rank(decade) + " " + babyNames[i++].Name + " and " + babyNames[i].Name);
            }
        }
    }
}
